package com.lesso.yjy.sunflower.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.yjy.sunflower.core.ApiResponse;
import com.lesso.yjy.sunflower.pojo.dto.AttendanceRuleDTO;
import com.lesso.yjy.sunflower.pojo.dto.AttendanceRuleQueryDTO;
import com.lesso.yjy.sunflower.pojo.vo.AttendanceRuleVO;
import com.lesso.yjy.sunflower.service.IAttendanceRuleService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/base/rule")
public class AttendanceRuleController {
    @DubboReference
    private IAttendanceRuleService attendanceRuleService;

    @PostMapping
    public ApiResponse<Long> save(@Valid @RequestBody AttendanceRuleDTO dto){
        return ApiResponse.<Long>ok().data(attendanceRuleService.save(dto)).build();
    }

    @GetMapping("/page")
    public ApiResponse<Page<AttendanceRuleVO>> page(AttendanceRuleQueryDTO dto){
        return ApiResponse.<Page<AttendanceRuleVO>>ok()
                .data(attendanceRuleService.page(dto))
                .build();
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<AttendanceRuleVO>> customPage(AttendanceRuleQueryDTO dto){
        return ApiResponse.<Page<AttendanceRuleVO>>ok().data(attendanceRuleService.customPage(dto)).build();
    }

    @DeleteMapping
    public ApiResponse<Object> delete(@RequestParam("id") Long id){
        Assert.notNull(id, "id不能为空");
        if (id < 0) {
            throw new RuntimeException("id不能小于0");
        }
        attendanceRuleService.deleteById(id);
        return ApiResponse.ok().build();
    }

    @PutMapping
    public ApiResponse<Object> update(@RequestBody AttendanceRuleDTO dto){
        attendanceRuleService.update(dto);
        return ApiResponse.ok().build();
    }
}
